from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from PIL import Image
from Unit_05.dama import Dama


driver = webdriver.Edge()
driver.get("http://10.59.9.12:7001/")
driver.maximize_window()
sleep(2)
driver.find_element(By.LINK_TEXT,'登录').click()
sleep(1)
driver.find_element(By.NAME,'username').send_keys('18238680385')
sleep(1)
driver.find_element(By.NAME,'password').send_keys('123456')
sleep(1)


# 获取验证码图片的元素
yzm = driver.find_element(By.XPATH,'//*[@id="theForm"]/div[3]/span[3]/img')
# 打印坐标
location = yzm.location
print(location)
# 打印大小
size = yzm.size
print(size)
# 截验证码页面大图 以便后边获取验证码图片位置
driver.save_screenshot('datu.png')
left = location['x']
height = location['y']
# 图片右侧 = 左侧坐标 + 图片的宽度
right = left + size['width']
# 图片底部 = 顶部坐标 + 图片的高度
bottom = height + size['height']
# 使用Image打开截取的大图,赋值给变量Image1,后续可以使用Image1进行操作 （Image1.show()展示图片）
Image1 = Image.open('datu.png')
# 该行是对Image1进行裁剪操作,crop方法接收一个包含四个坐标值的'元组'作为参数，用于指定裁剪区域 左上右下顺序
image1 = Image1.crop((left*1.5,height*1.5,right*1.5,bottom*1.5))
# 将裁剪后的图片保存为image2.png
image1.save('image2.png')

dm = Dama('18238680385','123456Aa','970644')
yzmdm = open('image2.png','rb').read()
jieguo = dm.PostPic(yzmdm,1902)
print(jieguo)
result = jieguo['pic_str']
print(f"我的打码结果:{result}")
# 输入验证码
driver.find_element(By.NAME,'code').send_keys(result)


sleep(3)
# 点击登录按钮
driver.find_element(By.XPATH,'//*[@id="theForm"]/div[5]/a').click()

sleep(6)